#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/4/9 23:11
# @Author : cjw
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import visibility_of_element_located
from selenium.webdriver.common.action_chains import ActionChains
from chaojiying import Chaojiying_Client
from time import sleep

# 初始化超级鹰
chaojiying = Chaojiying_Client('18367157420', 'qwer128201209q@', '915032')


def login_12306(username: str, password: str):
    """
    12306登录
    :param username: 用户名
    :param password: 密码
    :return:
    """
    # 如果程序被识别到了怎么办
    # 1、chrome的版本号如果小于88 在你启动浏览器的时候(此时没有加载任何网页内容)，向网页嵌入js代码，去掉webdriver
    # driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    #     'source': '''
    #     navigator.webdriver = undefined
    #       Object.defineProperty(navigator, 'webdriver', {
    #         get: () => undefined
    #       })
    #     '''
    # })
    # 2、chrome的版本大于88
    options = Options()
    prefs = dict()
    prefs['credentials_enable_service'] = False
    prefs['profile.password_manager_enabled'] = False
    options.add_experimental_option('prefs', prefs)  # 禁用密码保存弹出框
    options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 不显示自动化软件控制提示
    # 允许自动化控制，可以设置window.navigator.webdriver=false
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.set_capability('credentials_enable_service', False)
    options.set_capability('profile.password_manager_enabled', False)
    driver = webdriver.Chrome(options=options)
    # 不显示自动化软件控制提示
    driver.maximize_window()
    driver.get('https://kyfw.12306.cn/otn/resources/login.html')
    sleep(2)
    # 选择账号登录
    WebDriverWait(driver, 10).until(
        visibility_of_element_located((By.XPATH, '//li/a[contains(text(), "账号登录")]'))).click()

    # 验证码处理
    verify_img_el = WebDriverWait(driver, 10).until(
        visibility_of_element_located((By.XPATH, '//*[@id="J-loginImg"]')))
    # 验证码识别结果：x1,y1|x2,y2|x3,y3, 所以需要先分割‘|’，在分割‘,’
    result = chaojiying.PostPic(verify_img_el.screenshot_as_png, 9004)['pic_str']
    rs_list = result.split('|')
    # 输入用户名和密码
    WebDriverWait(driver, 10).until(
        lambda x: x.find_element_by_xpath('//*[@id="J-userName"]')).send_keys(username)
    WebDriverWait(driver, 10).until(
        lambda x: x.find_element_by_xpath('//*[@id="J-password"]')).send_keys(password)
    # 输入验证码
    for rs in rs_list:
        p_tmp = rs.split(',')
        x = int(p_tmp[0])
        y = int(p_tmp[1])
        # 要让鼠标移动到某一个位置后，然后进行点击
        # 通过事件链进行点击,move_to_element_with_offset: 偏移量移动,就是在某个查询的元素区域内移动鼠标
        ActionChains(driver).move_to_element_with_offset(verify_img_el, x, y).click().perform()
    WebDriverWait(driver, 10).until(
        lambda x: x.find_element_by_xpath('//*[@id="J-login"]')).click()
    # 拖拽滑块
    btn_el = WebDriverWait(driver, 10).until(
        lambda x: x.find_element_by_xpath('//*[@id="nc_1_n1z"]'))
    ActionChains(driver).drag_and_drop_by_offset(btn_el, 300, 0).perform()
    # 登录成功后，点击确定按钮
    WebDriverWait(driver, 10).until(
        lambda x: x.find_element_by_xpath('//div[@class="modal-ft"]/a')).click()
    sleep(5)
    driver.quit()


if __name__ == '__main__':
    login_12306('xxx', 'xxx')
